<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Weekdays extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('weekday');
	}
	public function ajax_weekday_get(){
		$weekdayEntity =  new CI_Week_day_entity();
		$weekday = $weekdayEntity->getWeekDayByCode($_REQUEST['code']);
		echo json_encode($weekday);
	}
	public function ajax_weekday_list(){
		$weekdayEntity =  new CI_Week_day_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$weekdayList = $weekdayEntity->getWeekDayListByLanguage($defaultLanguageId);
		$data['weekdayList'] = $weekdayList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/weekdays/index', $data);
	}
	public function ajax_weekday_save(){
		$weekdayNameList = $_REQUEST['weekday'];
		$weekdayOrderList = $_REQUEST['weekdayorder'];
		$weekdayNameDefault = '';
		foreach ($weekdayNameList as $key => $value) {
			if($value != null && $value != ""){
				$weekdayNameDefault = $value;
				break;
			}
		}
		$weekdayOrderDefault = 0;
		foreach ($weekdayOrderList as $key => $value) {
			if($value != null && $value != ""){
				$weekdayOrderDefault = $value;
				break;
			}
		}
		
		$result = array();
		if($weekdayNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_weekday_require');
		}else{
			$code = 0;
			foreach ($weekdayNameList as $key => $value) {
				$weekdayEntity =  new CI_Week_day_entity();
				if($value != null && $value != ''){
					$weekdayEntity->setWeekDay($value);
				}else{
					$weekdayEntity->setWeekDay($weekdayNameDefault);
				}
				if($weekdayOrderList[$key] != null){
					$weekdayEntity->setOrder($weekdayOrderList[$key]);
				}else{
					$weekdayEntity->setOrder($weekdayOrderDefault);
				}
								
				$weekdayEntity->setLanguageId($key);
				$weekdayEntity->setCode($code);
				$id = $weekdayEntity->save();
				if($code == 0){
					$code = $id;
					$weekdayEntity->setId($id);
					$weekdayEntity->setCode($code);
					$weekdayEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_weekday_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_weekday_update(){
		$weekdayNameList = $_REQUEST['weekday'];
		$weekdayOrderList = $_REQUEST['weekdayorder'];
		$weekdayIdList = $_REQUEST['weekday_id'];
		$weekdayCode = $_REQUEST['weekday_code'];
		$weekdayNameDefault = '';
		foreach ($weekdayNameList as $key => $value) {
			if($value != null && $value != ""){
				$weekdayNameDefault = $value;
				break;
			}
		}
		$weekdayOrderDefault = 0;
		foreach ($weekdayOrderList as $key => $value) {
			if($value != null && $value != ""){
				$weekdayOrderDefault = $value;
				break;
			}
		}
		$result = array();
		if($weekdayNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_weekday_require');
		}else{
			foreach ($weekdayNameList as $key => $value) {
				$weekdayEntity =  new CI_Week_day_entity();
				if($value != null && $value != ''){
					$weekdayEntity->setWeekDay($value);
				}else{
					$weekdayEntity->setWeekDay($weekdayNameDefault);
				}
				if($weekdayOrderList[$key] != null){
					$weekdayEntity->setOrder($weekdayOrderList[$key]);
				}else{
					$weekdayEntity->setOrder($weekdayOrderDefault);
				}
				$weekdayEntity->setLanguageId($key);
				$weekdayEntity->setCode($weekdayCode);
				$weekdayEntity->setId($weekdayIdList[$key]);
				$weekdayEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_weekday_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_weekday_delete(){
		$result = array();
		$weekdayEntity =  new CI_Week_day_entity();
		$result['result'] = $weekdayEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_weekday_delete_success');
		}else{
			$result['message'] = lang('escort_weekday_delete_error');
		}
		echo json_encode($result);
	}
}
?>